<?php

	class PagedResultSet{
		private $resultsize;
		private $results;
		private $pageSize;
		private $page;
		private $row = 0;
	
		function PagedResultSet($resultsize,$pageSize){
			if(!isset($_GET['resultpage']))
			$resultpage = 1;
			else
			$resultpage = $_GET['resultpage'];
	
			$this->resultsize = $resultsize;
			$this->pageSize = $pageSize;
	
			if ((int)$resultpage <= 0) $resultpage = 1;
			if ($resultpage > $this->getNumPages())
			$resultpage = $this->getNumPages();
			$this->setPageNum($resultpage);
		}

		public function getNumPages(){
			return ceil($this->resultsize /(float)$this->pageSize);
		}

		public function get_page_size(){
			return $this->pageSize;
		}

		public function set_results($r){
			$this->results = $r;
		}

		function setPageNum($pageNum){
			if ($pageNum > $this->getNumPages() or
			$pageNum <= 0) return FALSE;
			$this->page = $pageNum;
		}

		public function getPageNum(){
			return $this->page;
		}

		function isLastPage(){
			return ($this->page >= $this->getNumPages());
		}

		function isFirstPage(){
			return ($this->page <= 1);
		}

		function fetchLine(){
			if (!$this->results) return FALSE;
			if ($this->row >= $this->pageSize) return FALSE;
			if ($this->row >= count($this->results)) return FALSE;
			$pos = $this->row;
			$this->row++;
			return $this->results[$pos];
		}

		function getPageNav($queryvars = ''){
			$nav = '';
			if (!$this->isFirstPage()){
				$nav .= "<a href=\"?resultpage=".($this->getPageNum()-1).'&'.$queryvars.'">'._("Anterior").'</a> ';
			}
			if ($this->getNumPages() > 1){
				for ($i=1; $i<=$this->getNumPages(); $i++){
					if ($i==$this->page)
					$nav .= "$i ";
					else
					$nav .= "<a href=\"?resultpage={$i}&".$queryvars."\">{$i}</a> ";
				}
				if (!$this->isLastPage()){
					$nav .= "<a href=\"?resultpage=". ($this->getPageNum()+1).'&'.$queryvars.'">'._("Seguinte").'</a> ';
				}
			}
			return $nav;
		}
	}
	
?>
